package com.zh.business.service.web.common.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.business.service.web.common.IDCommonCodeService;
import com.zh.zhbootcommon.entity.common.CommonCode;
import com.zh.business.mapper.common.DCommonCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-24
 */
@Service
public class DCommonCodeServiceImpl extends ServiceImpl<DCommonCodeMapper, CommonCode> implements IDCommonCodeService {

    @Autowired
    DCommonCodeMapper codeMapper;

    @Override
    public Page<CommonCode> queryCodeList(Integer page, Integer limit, String type, String codeName) {
        Page<CommonCode> commonCodePage = new Page<>(page, limit);
        LambdaQueryWrapper<CommonCode> queryWrapper = new LambdaQueryWrapper();
        // 0-一级 1-二级
        if ("0".equals(type)) {
            queryWrapper.eq(CommonCode::getCPCode, null);
        }else if ("1".equals(type)){
            queryWrapper.gt(CommonCode::getCPCode, 0);
        }
        queryWrapper.like(CommonCode::getCName, codeName);
        commonCodePage = codeMapper.selectPage(commonCodePage, queryWrapper);
        commonCodePage.setTotal(commonCodePage.getRecords().size());
        return commonCodePage;
    }

    @Override
    public Integer insert(CommonCode commonCode) {
        return codeMapper.insert(commonCode);
    }

    @Override
    public boolean deleteChild(Integer cId) {
        boolean flag = false;
        Integer num = 0;
        LambdaQueryWrapper<CommonCode> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(CommonCode::getCPCode, cId);
        List<CommonCode> commonCodes = codeMapper.selectList(queryWrapper);
        List<Integer> cIds = Optional.ofNullable(commonCodes).orElse(new ArrayList<>())
                .stream().map(c -> c.getCId()).collect(Collectors.toList());
        if (cIds.size() > 0) {
            num = codeMapper.deleteBatchIds(cIds);
            if (num > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public CommonCode getCommonCode(CommonCode commonCode) {
        CommonCode commonNewCode = new CommonCode();
        LambdaQueryWrapper<CommonCode> queryWrapper = new LambdaQueryWrapper();
        if (commonCode.getCCode() > 0) {
            queryWrapper.eq(CommonCode::getCCode, commonCode.getCCode());
        }
        if (StrUtil.isNotBlank(commonCode.getCName())) {
            queryWrapper.eq(CommonCode::getCName, commonCode.getCName());
        }
        /**
         * isEmptyOfEntity 深层实体判断属性
         * isEmptyOfNormal 查询条件为空(不包含entity)
         * isEmptyOfWhere 查询条件为空(包含entity)
         */
        if (queryWrapper.nonEmptyOfNormal()) {
            commonNewCode = codeMapper.selectOne(queryWrapper);
        }

        return commonNewCode;
    }

    @Override
    public CommonCode getCommonCodeByCode(Integer cCode) {
        CommonCode commonNewCode = new CommonCode();
        LambdaQueryWrapper<CommonCode> queryWrapper = new LambdaQueryWrapper();
        if (cCode > 0) {
            queryWrapper.eq(CommonCode::getCCode, cCode);
        }
        if (queryWrapper.nonEmptyOfNormal()) {
            commonNewCode = codeMapper.selectOne(queryWrapper);
        }
        return commonNewCode;
    }
}
